package com.example.softwareengg;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ScreenB extends HeaderFooter implements OnClickListener {
	String serverIP =MyOverridenApp.getserverIP();
	 Socket client = MyOverridenApp.getClient();
static String shruti1;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		ViewGroup vg = (ViewGroup) findViewById(R.id.lldata);
        ViewGroup.inflate(ScreenB.this, R.layout.screenb, vg);
        
		
		//Set user name
		
		TextView userName1 = (TextView)findViewById(R.id.textUsername);
        
        
		//Get user's name
		
		
		shruti1 = MyOverridenApp.getUser();
	
		String shrutinew = "Welcome " + shruti1;
		
		Button request = (Button) findViewById(R.id.request);
		Button respond = (Button) findViewById(R.id.respond);
		Button log = (Button) findViewById(R.id.log);
		Button not = (Button) findViewById(R.id.not);
		Button home = (Button)findViewById(R.id.home);
		request.setOnClickListener(this);
		respond.setOnClickListener(this);
		log.setOnClickListener(this);
		not.setOnClickListener(this);
		
		
		//Underline text
	
		
		/*TextView textRequest = (TextView) findViewById(R.id.textRequest);
		TextView textRespond = (TextView) findViewById(R.id.textRespond);
		TextView textLog = (TextView) findViewById(R.id.textLog);
		TextView textNotification = (TextView) findViewById(R.id.textNotification);*/
		SpannableString content5 = new SpannableString(shrutinew);
		
		/*SpannableString content1 = new SpannableString("Request");
		SpannableString content2 = new SpannableString("Respond");
		SpannableString content3 = new SpannableString("Log");
		SpannableString content4 = new SpannableString("Notifications");
		content1.setSpan(new UnderlineSpan(), 0, content1.length(), 0);
		content2.setSpan(new UnderlineSpan(), 0, content2.length(), 0);
		content3.setSpan(new UnderlineSpan(), 0, content3.length(), 0);
		content4.setSpan(new UnderlineSpan(), 0, content4.length(), 0);*/
		content5.setSpan(new UnderlineSpan(), 0, content5.length(), 0);
		
		/*textRequest.setText(content1);
		textRespond.setText(content2);
		textLog.setText(content3);
		textNotification.setText(content4);*/
		userName1.setText(content5);
		Button button_logout = (Button) findViewById(R.id.Button_logout);
		button_logout.setOnClickListener(this);
		home.setOnClickListener(this);
		
	}
	 public void onClick(View v) {
	        switch (v.getId()) {
	        case R.id.home:
	        {
				   Intent intent = new Intent(ScreenB.this,ScreenB.class);
				    startActivity(intent);
				    break;

	        }

	        case R.id.buttonsettings:
	        {
				   Intent intent = new Intent(ScreenB.this,ScreenSettings.class);
				    startActivity(intent);
				    break;

	        }
	        case R.id.request:
	        	 
				 
				    
		    		Intent i = new Intent(ScreenB.this, Screenin.class);
		    		
		    		   startActivity(i);
		    		  // Socket client=MyOverridenApp.getClient();------------------------

	            break;
	        case R.id.respond:
	        	Intent i1 = new Intent(ScreenB.this, Screenout.class);
	    		
	    		   startActivity(i1);


	            break;
	            
	        case R.id.log:
	        	 Intent intent2 = new Intent(/*FirstActivity.this*/ v.getContext(), ScreenUsage.class);
				    startActivity(intent2);

	            break;
	            
	        case R.id.not:
	        	
	        {
	        	
	        	
	        	
	        	try{
	        	   
	        	    DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
	        		
	        		outToServer.writeBytes("notifications"+"\n");}catch(Exception e){
	        			e.printStackTrace();
	        		}
	        	    
	        	    
	        	    
	        	    try {
	        	    		
	        			Socket client = MyOverridenApp.getClient();
	        			
	        			//client1.setSoTimeout(5000);
	        			
	        			BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
	        			
	        				String serverReply1 = inFromServer.readLine();					
	        				//progressDialog.dismiss();
	        				
	        			
	        			MyOverridenApp.setserverdata(serverReply1);	
	        			System.out.println("shruti" +MyOverridenApp.getserverdata());
	        			
	        			//Toast.makeText(getApplicationContext(),"flag = "+flag, Toast.LENGTH_LONG).show();
	        			
	        			//linlaHeaderProgress.setVisibility(View.GONE);
	        			
	        		} catch (UnknownHostException e) {
	        			// TODO Auto-generated catch block
	        			e.printStackTrace();
	        		} catch (IOException e) {
	        			// TODO Auto-generated catch block
	        			e.printStackTrace();
	        		}

	        	Intent i2 = new Intent(ScreenB.this, Screennot.class);
	    		
	    		   startActivity(i2);

	            break;}
	            
	        case R.id.Button_logout:
	        {
	        	try{
	        	Socket client = MyOverridenApp.getClient();
	        	DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
	        	
				outToServer.writeBytes("signout\n");
				client.close();
				}
	        	catch(Exception e){
	        		e.printStackTrace();
	        	}
				   Intent intent = new Intent(ScreenB.this,ScreenA.class);
				    startActivity(intent);
				    break;

	        }
	        	default:
	            break;
	        }


	

	 }
	 @Override
	 public void onBackPressed() {
	 }

	

}
